package com.dys.flex;

import com.dys.mapper.AccountMapper;
import com.dys.mapper.SysUserMapper;
import com.dys.vo.AccountVO;
import com.dys.vo.UserVO;
import com.mybatisflex.core.query.QueryWrapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

import static com.dys.entity.table.AccountTableDef.ACCOUNT;
import static com.dys.entity.table.SysUserTableDef.SYS_USER;
import static com.mybatisflex.core.query.QueryMethods.*;

/**
 * @Author: 疾风
 * @ClassName: QueryAsData
 * @Description: 映射查询
 * @CreateDate: 2025/01/14
 * @Version: v1.0.0
 */
@SpringBootTest
public class QueryAsData {

    @Autowired
    private SysUserMapper userMapper;

    @Autowired
    private AccountMapper accountMapper;

    @Test
    void testSelectOneByQueryAs(){
        QueryWrapper queryWrapper = QueryWrapper.create()
                .select(SYS_USER.ALL_COLUMNS,if_(SYS_USER.STATUS.eq("1"),true_(),false_()).as("is_status"))
                .where(SYS_USER.ID.eq("2"));

        UserVO userVO = userMapper.selectOneByQueryAs(queryWrapper, UserVO.class);
        System.out.println(userVO);
    }

    @Test
    void testSelectOneByQueryAsVO(){
        QueryWrapper queryWrapper = QueryWrapper.create()
                .select(ACCOUNT.ALL_COLUMNS,if_(ACCOUNT.AGE.ge(18),true_(),false_()).as("is_adult"))
                .where(ACCOUNT.ID.eq(1));

        AccountVO accountVO = accountMapper.selectOneByQueryAs(queryWrapper, AccountVO.class);
        System.out.println(accountVO);
    }

    @Test
    void testSelectListByQueryAsVO(){
        QueryWrapper queryWrapper = QueryWrapper.create()
                .select(ACCOUNT.ALL_COLUMNS,if_(ACCOUNT.AGE.ge(18),true_(),false_()).as("is_adult"))
                .where(ACCOUNT.ID.ge(1));
        List<AccountVO> list = accountMapper.selectListByQueryAs(queryWrapper, AccountVO.class);
        list.forEach(System.out::println);
    }

    @Test
    void testSelectObjectByQueryAsVO(){
        QueryWrapper queryWrapper = QueryWrapper.create()
                .select(ACCOUNT.ALL_COLUMNS,if_(ACCOUNT.AGE.ge(18),true_(),false_()).as("is_adult"))
                .where(ACCOUNT.ID.eq(2));
        // 查询第一列返回的数据
        Integer isAdult = accountMapper.selectObjectByQueryAs(queryWrapper, Integer.class);
        System.out.println("第一列返回的数据: "+isAdult);
    }

    @Test
    void testSelectObjectListByQueryAsVO(){
        QueryWrapper queryWrapper = QueryWrapper.create()
                .select(ACCOUNT.USERNAME)
                .where(ACCOUNT.ID.ge(2));
        // 查询第一列返回的数据集合
        List<String> list = accountMapper.selectObjectListByQueryAs(queryWrapper, String.class);
        list.forEach(System.out::println);
    }
}
